<?php


if (!defined('IN_ANWSION'))
{
	die;
}

/**
 *  special 专辑 model 层
 * @author LSH  2013-01-11  10:01
 *
 */
class special_class extends AWS_MODEL
{
	
	/**
	 * 获取专辑的信息
	 * @param INT $special_id    // 专辑ID
	 * @param INT $cache           // 缓存标记
	 * @return array|                  // 专辑的信息
	 */
	public function get_special_info_by_id($special_id,$cache = true)
	{
		if (! $special_id)
		{
			return false;
		}
		
		if (!$cache)
		{
			$special[$special_id] = $this->fetch_row('special', "id = " . intval($special_id));
		}
		else
		{
			static $special;
		
			if ($special[$special_id])
			{
				return $special[$special_id];
			}
				
			$special[$special_id] = $this->fetch_row('special', "id = " . intval($special_id));
		}
		return $special[$special_id];
	}
	/**
	 * 获取专辑的信息,后台
	 * @param INT $special_id    // 专辑ID
	 * @param INT $cache           // 缓存标记
	 * @return array|                  // 专辑的信息
	 */	
	public function get_special_by_id($feature_id)
	{
		if(!$feature_id)
		{
			return false;
		}
		$feature_ids = array();
	
		if (is_array($feature_id))
		{
			$feature_ids = $feature_id;
		}
		else
		{
			$feature_ids[] = intval($feature_id);
		}
		if ($rs = $this->fetch_all('special', 'id IN (' . implode(',', $feature_ids) . ')'))
		{
			$data = array();
				
			foreach($rs as $key => $val)
			{
				if (!$val['url_token'])
				{
					$rs[$key]['url_token'] = $val['id'];
				}
	
				$data[$val['id']] = $rs[$key];
			}
		}
		if (is_array($feature_id))
		{
			return $data;
		}
		else
		{
			return $data[$feature_id];
		}
	}	
	/**
	 * 获取专辑列表，分页
	 * @param INT $start    // 
	 * @param INT $limit
	 * @return array|multitype:
	 */
	public function get_special_list($start = 0,$limit = 10)
	{
		$where = array();
		$sort_key = 'id';
		$order = 'DESC';
		
		$page = 0;
		$per_page = 15;
		
		$special_info_list = $this->fetch_page('special', implode(' AND ', $where), $sort_key . ' ' . $order, $page, $per_page);
		if ($special_info_list)
		{
			return $special_info_list;
		}
		else
		{
			return array();
		}
		
	}
	/**
	 * 根据关键字获取网站列表
	 * @param INT $start    //
	 * @param INT $limit
	 * @return array|multitype:
	 */	
	public function get_search_site($keyword){
		$sql="select question_id,question_content from aws_question where question_content like '%".$keyword."%'";
	//	$site_list = $this->db()->query($sql);
		$site_list = $this->query_all($sql);
	//	print_r($site_list);
		$site_str="[";
		foreach ($site_list as $item){
			$site_str.="{sid:'".$item["question_id"]."',sitename:'".$item["question_content"]."'},";
		}
		$slen=strlen($site_str);
		if($slen>1){
			$site_str=substr($site_str,0,$slen-1);
		}	
		$site_str.="]";
		return $site_str;
	}
	public function get_special_list_by_question_id($question_id)
	{
		$special_info_list = array();		
		$special_question_list = $this->fetch_all('special_question', 'question_id='.intval($question_id));
	
		if ($special_question_list)
		{
			foreach ($special_question_list as $special_question)
			{
				$special_info_list[$special_question['special_id']] = $this->get_special_info_by_id($special_question['special_id']);

			}
		}
		else
		{
			return array();
		}
		
		return $special_info_list;
	
	}
	
	/**
	 * 获取专辑列表，分页,后台
	 * @param INT $start    //
	 * @param INT $limit
	 * @return array|multitype:
	 */	
	public function get_special_list1($where = null, $count = false, $order = null, $limit = null)
	{
		if ($count)
		{
			return $this->count('special', $where);
		}
		else
		{
			if ($feature_list = $this->fetch_all('special', $where, $order, $limit))
			{
				foreach($feature_list as $key => $val)
				{
					if (!$val['url_token'])
					{
						$feature_list[$key]['url_token'] = $val['id'];
					}
				}
	
				return $feature_list;
			}
			else
			{
				return array();
			}
		}
	}	
	/**
	 * 获取专辑 的问题列表
	 * @param INT $special_id  // 专辑ID
	 * @return unknown
	 */
	public function get_special_question_list_by_special_id($special_id)
	{
		$where = array();
		$sort_key = 'id';
		$order = 'DESC';
		$page = 0;
		$per_page = 15;
		
		$where[] = 'special_id = '.$special_id;
		
		$special_question_list = $this->fetch_page('special_question', implode(' AND ', $where), $sort_key . ' ' . $order, $page, $per_page);
		if ($special_question_list)
		{
			return $special_question_list;
		}
		else
		{
			return $special_question_list;
		}
	}
	/**
	 * 获取专辑 的网站列表
	 * @param INT $special_id  // 专辑ID
	 * @return unknown
	 */	
	public function get_specialSite_list($where = null, $count = false, $order = null, $limit = null)
	{
		if ($count)
		{
			return $this->count('special_question', $where);
		}
		else
		{
			$sql='select * from aws_special_question as s inner join aws_question as q on s.question_id=q.question_id where 1=1 and   s.special_id='.$where.' order by '.$order.' limit '.$limit;
			if ($feature_list = $this->query_all($sql))
			{
			//	print_r($feature_list);
				$sql1="select title from aws_special where id=".$where;
				$title=$this->query_row($sql1);
				$feature_list[0]["title"]=$title["title"];
				return $feature_list;
			}
			else
			{
				return array();
			}
		}
	}
	/**
	 * 获取专辑网站信息
	 * 
	 */
	public function get_qs_info($qs_id)
	{
		$qs_info=$this->fetch_row('special_question', "id = " . intval($qs_id));
		$sql1="select title from aws_special where id=".$qs_info["special_id"];
		$title=$this->query_row($sql1);
		$qs_info["title"]=$title["title"];	
		$sql2="select question_content from aws_question where question_id=".$qs_info["question_id"];
		$sitename=$this->query_row($sql2);
		$qs_info["sitename"]=$sitename["question_content"];			
		return $qs_info;
	}
	/**
	 * 更新专辑信息
	 * @param INT   $special_id   // 专辑ID
	 * @param array $data           // 待修改的专辑信息
	 * @return boolean
	 */
	public function update_special_info($special_id,$data)
	{
		if ($special_id == 0 || empty($data))
		{
			return false;
		}
		
		$toUpdate = array();
		if (isset($data['title'])&& is_null($data['title']))
		{
			$toUpdate['title'] = $data['title'];
		}
		
		if (isset($data['desp'])&& is_null($data['desp']))
		{
			$toUpdate['desp'] = $data['desp'];
		}
		
		if (isset($data['url'])&& is_null($data['url']))
		{
			$toUpdate['url'] = $data['url'];
		}
		
		if (isset($data['img_url'])&& is_null($data['img_url']))
		{
			$toUpdate['img_url'] = $data['img_url'];
		}
		
		return $this->update('question_img', $toUpdate, 'id = ' . intval($special_id));
	}
	/**
	 * 更新专辑信息,后台
	 * @param INT   $special_id   // 专辑ID
	 * @param array $data           // 待修改的专辑信息
	 * @return boolean
	 */	
	public function update_special($special_id, $update_arr)
	{
		return $this->update('special', $update_arr, 'id = ' . $special_id);
	}
	public function update_book($special_id, $siteid)
	{
		$sql="select question_cover_id from aws_question where question_id=".$siteid;
		$site_img_id=$this->query_row($sql);
		$site_img_id=$site_img_id['question_cover_id'];
		$sql="select img_url from aws_question_img where id=".$site_img_id;
		$site_img=$this->query_row($sql);
		$site_img=$site_img['img_url'];	
		$update_arr = array(
				'img_url' => $site_img,
		);			
		return $this->update('special', $update_arr, 'id = ' . $special_id);
	}	
	/**
	 * 更新专辑信息,后台
	 * @param INT   $special_id   // 专辑ID
	 * @param array $data           // 待修改的专辑信息
	 * @return boolean
	 */
	public function update_qs($qs_id, $update_arr)
	{
		return $this->update('special_question', $update_arr, 'id = ' . $qs_id);
	}	
	function check_url_token($url_token, $feature_id)
	{
		return $this->count('feature', "url_token = '" . $this->quote($url_token) . "' AND id != " . intval($feature_id));
	}
	//检查本专辑下是否存在某网站	
	function check_site_token($siteId, $special_id)
	{
		return $this->count('special_question', "question_id  = " . $siteId . " AND special_id  = " . intval($special_id));
	}		
	/**
	 * 添加专辑信息
	 * @param array $data   // 专辑信息
	 */
	public function add_special($data)
	{
		$toAdd = array();
		
		if (isset($data['title'])&& !is_null($data['title']))
		{
			$toAdd['title'] = $data['title'];
		}
		else
		{
			$toAdd['title'] = "";
		}
		
		if (isset($data['description'])&& !is_null($data['description']))
		{
			$toAdd['description'] = $data['description'];
		}
		else
		{
			$toAdd['description'] = "";
		}
		
		if (isset($data['img_url'])&& !is_null($data['img_url']))
		{
			$toAdd['img_url'] = $data['img_url'];
		}
		else
		{
			$toAdd['img_url'] = "";
		}
		
// 		if (isset($data['css'])&& is_null($data['css']))
// 		{
// 			$toAdd['css'] = $data['css'];
// 		}
// 		else
// 		{
// 			$toAdd['css'] = "";
// 		}
		if (isset($data['url_token'])&& !is_null($data['url_token']))
		{
			$toAdd['url_token'] = $data['url_token'];
		}
		else
		{
			$toAdd['url_token'] = "";
		}		
		$toAdd['add_time'] = time();
		 return $this->insert('special',$toAdd);
	}	
	/**
	 *  添加专辑网站对应表记录
	 * @param INT $special_id     // 专辑ID
	 * @param INT $question_id   // 问题ID
	 */
	public function add_special_site($data)
	{
		return $this->insert('special_question',$data);
	}
	
	/**
	 * 删除专辑
	 * @param INT $special_id
	 */
	public function del_special_info($special_id)
	{
		return $this->delete('special',  " id = " . intval($special_id));
	}
	
	/**
	 * 删除专辑问题对于表 记录
	 * @param INT $special_id        // 专辑ID
	 * @param INT $question_id     //  问题ID
	 */
	public function del_special_question($question_id)
	{
	//	return $this->delete('special_question', "question_id = " . intval($question_id) . " AND special = " . intval($special_id));
		return $this->delete('special_question', "question_id = " . intval($question_id) );
	}
}